﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using MvcApplication6.DAL.Helper;
using MvcApplication6.Models;

namespace MvcApplication6.DAL
{
    public class EventDAL
    {
        //-------------------EventSearchTitleById_ce-------------------
        public List<EventModel> EventSearchTitleById_ce(int idCe)
        {

            List<EventModel> lst = new List<EventModel>();

            EventModel obj = null;

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@id_ce", idCe));


            SqlDataReader dr = SQLHelper.ExecuteReader("spEventSearchTitleById_ce", parameterList);
            while (dr.Read())
            {
                obj = new EventModel();

                obj.Id = int.Parse(dr["id"].ToString());
                obj.IdCe = int.Parse(dr["id_ce"].ToString());
                obj.Title = dr["title"].ToString();
                //obj.Title2 = dr["title2"].ToString();
                obj.Image = dr["image"].ToString();
                //obj.StartDate = dr["start_date"].ToString();
                //obj.EndDate = dr["end_date"].ToString();
                //obj.Content = dr["content"].ToString();

                lst.Add(obj);
            }
            dr.Close();
            return lst;

        }

        //-------------------EventSearchById-------------------
        public EventModel EventSearchById(int id)
        {


            EventModel obj = null;

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@id", id));


            SqlDataReader dr = SQLHelper.ExecuteReader("spEventSearchById", parameterList);

            while (dr.Read())
            {
                obj = new EventModel();

                obj.Id = int.Parse(dr["id"].ToString());
                obj.IdCe = int.Parse(dr["id_ce"].ToString());
                obj.Title = dr["title"].ToString();
                obj.Title2 = dr["title2"].ToString();
                obj.Image = dr["image"].ToString();
                obj.StartDate = dr["start_date"].ToString();
                obj.EndDate = dr["end_date"].ToString();
                obj.Content = dr["content"].ToString();

                obj.Title2 = dr["title2"].ToString();

                obj.IdCe = int.Parse(dr["id_ce"].ToString());

                obj.Image = dr["image"].ToString();

                obj.StartDate = dr["start_date"].ToString();

                obj.EndDate = dr["end_date"].ToString();

                obj.Content = dr["content"].ToString();
            }
            dr.Close();
            return obj;
        }

        //-------------------EventSearchById-------------------
        public List<EventModel> EventSearchSale(int flag)
        {
            List<EventModel> lstEvent = new List<EventModel>();

            EventModel obj = null;

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@flag", flag));

            SqlDataReader dr = SQLHelper.ExecuteReader("spEventSearchSale", parameterList);

            while (dr.Read())
            {
                obj = new EventModel();

                obj.Id = int.Parse(dr["id"].ToString());
                obj.IdCe = int.Parse(dr["id_ce"].ToString());
                obj.Title = dr["title"].ToString();
                obj.Title2 = dr["title2"].ToString();
                obj.Image = dr["image"].ToString();

                lstEvent.Add(obj);
            }
            dr.Close();
            return lstEvent;
        }

        //---------------------EventInsert-----------------------------
        public int EventInsert(int id_ce, string title, string title2, string image, String start_date, String end_date, string content)
        {
            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@id_ce", id_ce));
            parameterList.Add(new SqlParameter("@title", title));
            parameterList.Add(new SqlParameter("@title2", title2));
            parameterList.Add(new SqlParameter("@image", image));
            parameterList.Add(new SqlParameter("@start_date", start_date));
            parameterList.Add(new SqlParameter("@end_date", end_date));
            parameterList.Add(new SqlParameter("@content", content));

            int i = SQLHelper.ExecuteNonQuery("spEventInsert", parameterList);

            return i;
        }

        //---------------------EventUpdate-----------------------------
        public int EventUpdate(int id, int id_ce, string title, string title2, string image, String start_date, String end_date, string content)
        {
            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@id", id));
            parameterList.Add(new SqlParameter("@id_ce", id_ce));
            parameterList.Add(new SqlParameter("@title", title));
            parameterList.Add(new SqlParameter("@title2", title2));
            parameterList.Add(new SqlParameter("@image", image));
            parameterList.Add(new SqlParameter("@start_date", start_date));
            parameterList.Add(new SqlParameter("@end_date", end_date));
            parameterList.Add(new SqlParameter("@content", content));

            int i = SQLHelper.ExecuteNonQuery("spEventUpdate", parameterList);

            return i;
        }

        //---------------------EventDelete-----------------------------
        public int EventDelete(int id)
        {
            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@id", id));

            int i = SQLHelper.ExecuteNonQuery("spEventDelete", parameterList);

            return i;
        }
    }
}
